home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0010_DEC2BIN1.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  657b  |  27 lines

  1. {
  2. >  I need to transfer decimal into binary using TURBO PASCAL.
  3. >  One way to do this is to use the basic algorithm, dividing
  4. >  by 2 over and over again. if the remainder is zero the
  5. >  bit is a 0, else the bit is a 1.
  6. >
  7. >  However, I was wondering if there is another way to convert
  8. >  from decimal to binary using PASCAL. Any ideas?
  9.  
  10. As an 8-bit (ie. upto 255) example...
  11. }
  12.  
  13.   Function dec2bin(b:Byte) : String;
  14.   Var bin : String[8];
  15.       i,a : Byte;
  16.   begin
  17.    a:=2;
  18.    For i:=8 downto 1 do
  19.     begin
  20.      if (b and a)=a then bin[i]:='1'
  21.                     else bin[i]:='0';
  22.      a:=a*2;
  23.     end;
  24.     dec2bin:=bin;
  25.   end;
  26.  
  27.